original_header = "";

$(document).ready(function() {
  
  original_header = $("#rw-col-headers").html();

	$("#user-faves li").click(function(){
		$("#user-contacts").fadeOut();
		$("#user-messages").fadeOut();
		$("#user-rewinds").fadeOut();

		$("#rw-col-headers").html("<h3 id='head-rewinds'>REWINDS FAVORITOS</h3><h3>&nbsp;&nbsp;</h3><h3>&nbsp;&nbsp;</h3><h3>TOP REWINDS</h3>");
		show_post( $(this).attr("class") );
	});//onclick

	$("#user-rewinds li").click(function(){
		$("#user-faves").fadeOut();
		$("#user-messages").fadeOut();
		$("#user-contacts").fadeOut();

		$("#rw-col-headers h3").fadeOut();
    show_post($(this).attr("class"));
	});//onclick

	$("#user-contacts li").click(function(){
		$("#user-faves").fadeOut();
		$("#user-messages").fadeOut();
		$("#user-rewinds").fadeOut();

		$("#rw-col-headers h3").fadeOut();

		show_person( $(this).attr("class") );
	});//onclick


	$("#user-messages li").click(function(){
		$("#user-faves").fadeOut();
		$("#user-contacts").fadeOut();
		$("#user-rewinds").fadeOut();

		$("#rw-col-headers h3").fadeOut();

		$.fancybox.showActivity();
		var url = "/rest/message/" + $(this).attr("class") + ".htm";

    		$.ajax({
			    type	: "GET",
			    cache	: false,
			    url		: url,
			    success: function(data) {
					$.fancybox(data, {
	      			'autoDimensions'  	: false,
	    		  	'width'         	: 550,
		  		  	'height'        	: 350,
		  		  	'transitionIn'	  	: 'elastic',
		  		  	'transitionOut'	  	: 'elastic',
		  		  	'scrolling'         : 'no',
		  		  	'overlayShow'       : false,
		  		  	'onClosed'			: function(){ showAll(); }
	  		    	});
			    }
	    	});//ajax
	});//onclick


    $("#edit-profile").click(function(){
        $("#profile-view").hide();
        //d = new Date();
        //$("#profile-avatar-edit").attr("src", "assets/user-pic.jpg?"+d.getTime());
        $("#profile-edit").show();
    });
    
    $("#cancel-edit-profile").click(function(){
      $("#profile-edit").hide();
      $("#profile-view").show();
    });
    
    $("#view-profile").click(function(){
        
    	/* validaciones client-side */
        if($("#profile-in-day").val() <= 0 || $("#profile-in-month").val() <= 0 || $("#profile-in-year").val() <= 0){
        	alert("Ingrese su fecha de cumpleaños");
        	return false;
        }
        
        var bday = $("#profile-in-day").val() + "/" + $("#profile-in-month").val() + "/" +  $("#profile-in-year").val();
        $("#profile-in-birthday").val(bday);
        $.fancybox.showActivity();
        /* Enviar cambios para guardar en la DB */
        $.post("/user/details", $("#profile-details").serializeArray(), function(data) {
        	if(data != "OK"){
            	alert("error ocurred:" + data);
          } else {
            var today = new Date();
            $("#profile-edit").hide();
	        	
            $("#profile-name").text( $("#profile-in-name").val() );
            $("#profile-age").text( today.getFullYear() - $("#profile-in-year").val());
            $("#profile-nationality").text( $("#profile-in-nationality").val() );
            $("#profile-livedin").text( $("#profile-in-livedin").val() );
            $("#profile-exp").text( $("#profile-in-exp").val() );
            $("#profile-hobbies").text( $("#profile-in-hobbies").val() );
            $("#profile-bestoflife").text( $("#profile-in-bestoflife").val() );
            $("#profile-placetoknow").text( $("#profile-in-placetoknow").val() );
            $("#profile-avatar").attr( "src", $('#profile-avatar-edit').attr('src') );
            $("#profile-view").show();
	            
            $.fancybox.hideActivity();
          }
	    });
    });

    $("#edit-profile-image a").click(function(){
      $.fancybox({
        'autoDimensions'  	: false,
        'width'           	: 420,
        'height'           	: 200,
        'transitionIn'	  	: 'fade',
        'transitionOut'	  	: 'fade',
        'scrolling'         : 'no',
        'type'				: 'iframe',
        'href'				: '/user/avatar'
      });
    });

});//onready

function show_person(name){
  var url = "/rest/user/name/" + name + ".htm";
  $.fancybox.showActivity();
  $.ajax({
    type	: "GET",
    cache	: false,
    url		: url,
    success: function(data) {
      $.fancybox(data,
        {
          'autoDimensions'  	: false,
          'width'         	: 850,
          'height'        	: 450,
          'transitionIn'	  	: 'elastic',
          'transitionOut'	  	: 'elastic',
          'scrolling'         : 'no',
          'overlayShow'       : false,
          'onClosed'			: function(){ showAll(); }
        }
      );
    }
  });//ajax  
}

function showAll(){
	$("#user-contacts").fadeIn();
	$("#user-faves").fadeIn();
	$("#user-rewinds").fadeIn();
	$("#user-messages").fadeIn();
	$("#rw-col-headers").html(original_header);
	$("#rw-col-headers h3").fadeIn();
}
